﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Phone.Controls;

namespace Gre_Wp7
{
    public partial class MainPage : PhoneApplicationPage
    {
        Word wd;

        DispatcherTimer dt = new DispatcherTimer();

        Engine engine = new Engine();

        bool meaningShow = true;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            wd = new Word();
            rectangle1.Width = 0;

            
            
            PageInit();
        }

        void dt_Tick(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            PageDown();

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            PageUp();
            
        }

        private void PageDown()
        {
            CWord[] cw = wd.SubPageArray();

            PageIndex.Text = wd.CurPage.ToString();
            listBox1.Items.Clear();
            int num = 1;
            foreach (CWord c in cw)
            {

                listBox1.Items.Add(string.Format("[{0}] {1}", num + wd.CurPage * 5, c.English));
                if (meaningShow == true)
                    listBox1.Items.Add(c.Meaning);
                else
                    listBox1.Items.Add("   ");
                num++;
            };
        }

        void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void PageInit()
        {
            CWord[] cw = wd.GetPageArray(wd.CurPage);
            PageIndex.Text = wd.CurPage.ToString() ;
            listBox1.Items.Clear();
            int num = 1;
            foreach (CWord c in cw)
            {

                listBox1.Items.Add(string.Format("[{0}] {1}", num + wd.CurPage * 5, c.English));

                if(meaningShow == true)
                    listBox1.Items.Add(c.Meaning);
                else
                    listBox1.Items.Add("   ");
                num++;
            };
        }

        private void PageUp()
        {
            //int bestPage = engine.GetBestPage();
            CWord[] cw = wd.AddPageArray();
            PageIndex.Text = wd.CurPage.ToString();
            listBox1.Items.Clear();
            int num = 1;
            foreach (CWord c in cw)
            {

                listBox1.Items.Add(string.Format("[{0}] {1}", num + wd.CurPage * 5, c.English));
                if (meaningShow == true)
                    listBox1.Items.Add(c.Meaning);
                else
                    listBox1.Items.Add("   ");
                num++;
            };
            //engine.RememberPage(bestPage);
        }

        private void OnFlick(object sender, FlickGestureEventArgs e)
        {
            if (e.Direction == System.Windows.Controls.Orientation.Horizontal)
            {
                if (e.Angle > 270 || e.Angle < 90)
                    PageDown();
                else
                    PageUp();
            }
        }

        private void PageIndex_MouseEnter(object sender, MouseEventArgs e)
        {
            if (meaningShow == false)
            {

                CWord[] cw = wd.GetPageArray(wd.CurPage);

                PageIndex.Text = wd.CurPage.ToString();
                listBox1.Items.Clear();
                int num = 1;



                foreach (CWord c in cw)
                {

                    listBox1.Items.Add(string.Format("[{0}] {1}", num + wd.CurPage * 5, c.English));
                    listBox1.Items.Add(c.Meaning);
                    num++;
                };
                meaningShow = true;
                return;
            }
            else
            {
                
                CWord[] cw = wd.GetPageArray(wd.CurPage);

                PageIndex.Text = wd.CurPage.ToString();
                listBox1.Items.Clear();
                int num = 1;



                foreach (CWord c in cw)
                {

                    listBox1.Items.Add(string.Format("[{0}] {1}", num + wd.CurPage * 5, c.English));
                    listBox1.Items.Add("  ");
                    num++;
                };
                meaningShow = false;
                return;
            }

            
        }
  


    }
}